package com.yesep.learn.comm.middle.util;

import com.yesep.learn.comm.middle.config.Config;
import org.python.core.PyDictionary;
import org.python.core.PyJavaType;
import org.python.core.PyObject;
import org.python.core.PyString;

public class CommUtil {
    private static final String SCRIPT_PATH = "script/";
    private static final String HANDLE_METHOD = "handle";

    public static String getScript(String pyfile) {
        if (!pyfile.startsWith(SCRIPT_PATH)) {
            pyfile = SCRIPT_PATH + pyfile;
        }
        return pyfile;
    }

    public static Config getConfig() {
        Config config = new Config();
        config.setVersion("1.0");
        config.setRemark("中间件配置123");
        return config;
    }

    public static Boolean handle(String pyfile, String data) {
        PyObject config = PyJavaType.wrapJavaObject(getConfig());
        JythonFactory jython = JythonFactory.getInstance();
        Boolean bool = jython.execute(getScript(pyfile), HANDLE_METHOD, Boolean.class, config, new PyString(data));
        return bool;
    }
}
